areahelper = {}

-- Cargas iniciales.
local ESTEMOD = minetest.get_current_modname()
dofile(minetest.get_modpath(ESTEMOD) .. "/functions.lua")
dofile(minetest.get_modpath(ESTEMOD) .. "/marcador.lua")
dofile(minetest.get_modpath(ESTEMOD) .. "/tools.lua")
dofile(minetest.get_modpath(ESTEMOD) .. "/hudelements.lua")

-- 
areahelper.claim_size = {x=2, y=2, z=2};

-- 
-- Objeto que llama a formspec (on_rightclick ? )
-- 
-- Datos quedan en el objeto
-- 
-- 
-- on_use -> debe seleccionar la caja que ha sido configurada.
--              solo si el usuario tiene el privilegio.
-- 
-- Craft del objeto helper.
--


-- -------------------------------
-- PLANTILLA AREA ----------------

areahelper.dimensionformspec = "formspec_version[4]"
    .. "size[12,6]"
    .. "position[0.5,0.5]"
    .. "anchor[0.5,0.5]"
    ;

local formspec =  "formspec_version[4]"
.. "size[12,6]"
.. "position[0.5,0.5]"
.. "anchor[0.5,0.5]"
;

--[[
minetest.register_tool("areahelper:plantillaarea", {
    description = "Plantilla para establecer areas",
    inventory_image = "square_yellow.png",
    tool_capabilities = {
        full_punch_interval = 1.5,
        max_drop_level = 1,
        groupcaps = {
        },
        damage_groups = {},
    },
   
    -- on_use = function(itemstack, user, pointed_thing),
    -- default: nil
    -- When user pressed the 'punch/mine' key with the item in hand.
    -- Function must return either nil if inventory shall not be modified,
    -- or an itemstack to replace the original itemstack.
    -- e.g. itemstack:take_item(); return itemstack
    -- Otherwise, the function is free to do what it wants.
    -- The user may be any ObjectRef or nil.
    -- The default functions handle regular use cases.
    
    on_use = function (itemstack, user, pointed_thing)
        minetest.log("USADO!");

        local tal, miss = minetest.check_player_privs(user, "server", "fly");
        -- minetest.log(dump(tal) .. " " .. dump(miss));
        
        --minetest.log(itemstack.get_description());
        --minetest.log(dump2(user));
        --minetest.log(minetest.serialize(pointed_thing));
        --minetest.log("x-------------x");

        local cabecera = "Vamos a intentarlo.";
        local form = {
            formspec,
            "label[0.375,0.5;", minetest.formspec_escape(cabecera), "]",
            "field[0.375,1.25;2 ,0.8;ancho;Ancho;]",
            "field[0.375,3.25;2,0.8;largo;Largo;]",
            "field[6,1.25;2,0.8;alto;Alto;]",
            "field[6,3.25;2,0.8;soterrado;Soterrado;]",
            "button_exit[4.5,5;3,0.8;setear;Setear]"
        };
        -- minetest.log(dump(placer:is_player()) .. " name " .. );
        
        local salida = minetest.show_formspec(user:get_player_name(), "areahelper:guesform", table.concat(form, ""));
        
        -- local meta = itemstack:get_meta();
        --minetest.log(dump(meta));
        -- minetest.log("SALIDA " .. dump(salida));
        -- RECIBIR LOS DATOS DEL FORMSPEC Y ALMACENARLOS EN EL MISMO OBJETO PORQUE SERÁN UTILIZADOS EN EL FORMSPEC.

        
        return nil; -- No cambiar el objeto de la mano.
    end,
    
    --      on_place = function(itemstack, placer, pointed_thing),
    -- When the 'place' key was pressed with the item in hand
    -- and a node was pointed at.
    -- Shall place item and return the leftover itemstack
    -- or nil to not modify the inventory.
    -- The placer may be any ObjectRef or nil.
    -- default: minetest.item_place
    
    
    -- on_place = function (itemstack, placer, pointed_thing)
        minetest.log("RIGHTCLICK! Trying to show meta");
        --dump(placer:get_wielded_item():get_meta():to_table()
        --local data = itemstack:get_meta():to_table().fields;
        -- local data = placer:get_wielded_item():get_meta():to_table().fields;
        -- minetest.log(dump(data));
        
        --minetest.log(itemstack.get_description());
        --minetest.log(dump2(placer));
        --minetest.log(minetest.serialize(pointed_thing));
        --minetest.log("+------------+");

        return nil; -- to not loose the object.
    end
});
]]--









--[[
minetest.register_craft({
    output = "areahelper:entityplacer",
    recipe = {
        {"default:paper", "", "" },
        {"default:paper", "default:stick", "default:stick" },
        {"","",""}

    }
})
]]--
-- COPIADO WORLDEDIT
local init_sentinel = "new" .. tostring(math.random(99999))
----------------------

--[[
local TryEntity = {
    initial_properties = {
        hp_max = 1,
        physical = true,
        collide_with_objects = false,
        collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
        visual = "wielditem",
        visual_size = {x = 0.4, y = 0.4},
        textures = {""},
        spritediv = {x = 1, y = 1},
        initial_sprite_basepos = {x = 0, y = 0},
    },

    message = "Default message",
} ]]--




-- AYUDA VISUAL DEL AREA
--[[
minetest.register_entity("areahelper:marker",{
    initial_properties = {
		hp_max = 1,
		visual = "upright_sprite",
		visual_size = {x = 1, y = 1},
		physical = false,
		textures = {"square_orange.png"},
	},
	on_activate = function(self, staticdata, dtime_s)
		minetest.after(3, function()
			self.object:remove()
		end)
	end,
})
]]--

local pc = areahelper.centro;
local size = 8;
--[[
minetest.register_node("areahelper:mark", {
	tiles = {"square_blue.png"},
	groups = {dig_immediate=3, not_in_creative_inventory=1},
	drop = "",
	use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
	walkable = false,
	drawtype = "nodebox",
	node_box = {
		type = "fixed",
		fixed = {
			{-size-.5, -size-.5, -size-.5,	-size-.5, size+.5,  size-.5},
			{-size-.5, -size-.5,  size-.5,	 size-.5, size+.5,  size-.5},
			{ size-.5, -size-.5, -size-.5,	 size-.5, size+.5,  size-.5},
			{-size-.5, -size-.5, -size-.5,	 size-.5, size+.5, -size-.5}
		}
	},
})
]]--

-- Fin del archivo
print("[MOD] Loaded areahelper. Mod made by Oscoder.");